package NC;

import java.util.ArrayList;
import java.util.Arrays;


// 回溯典型（排列，无重复）
public class NC43 {
    private ArrayList<ArrayList<Integer>> ans = new ArrayList<>();
    public ArrayList<ArrayList<Integer>> permute(int[] num) {
        Arrays.sort(num);
        backtrace(0,num,new boolean[num.length],new ArrayList<Integer>());
        return ans;
    }
    public void backtrace(int start,int[] num,boolean[] flags,ArrayList<Integer> temp){
        if(num.length<temp.size()) return;
        if(num.length==temp.size()) ans.add(new ArrayList<>(temp));
        for (int i = 0; i < num.length; i++) {
//            if(flags[i]) continue;
//            flags[i] = true;
            if(temp.contains(num[i])) continue;
            temp.add(num[i]);
            backtrace(start+1,num,flags,temp);
            temp.remove(temp.size()-1);
//            flags[i] =false;
        }
    }
}


class  Test4{
    public static void main(String[] args) {
        NC43 nc43 = new NC43();
        System.out.println(nc43.permute(new int[]{1, 2, 3}));
    }
}